<?php
/**
 * @author hequan@tianpin.com
 * @date	2013-08-05
 * @todo	获取京东订单
 */

require dirname(dirname(dirname(dirname(__FILE__)))) . '/www/index.php';
require ROOT_PATH . '/lib/openapi/jingdong/JingdongApi2.php';

error_reporting(E_ALL|E_STRICT);
ini_set("display_errors","on");
set_time_limit(0);
ini_set('memory_limit', -1);

// getJdOrders.php "2013-08-05 09:00:00" "2013-08-05 10:00:00"
// getJdOrders.php 1
// getJdOrders.php 10
if(count($argv) != 3 && count($argv) != 2){
	printf("%s (start_date end_date)|difftime\n", $argv[0]);
	exit(1);
}

if(count($argv) == 3){
	$start_date = $argv[1];
	$end_date	= $argv[2];
} else if(count($argv) == 2){
	$start_date	= date("Y-m-d H:i:s", time() - $argv[1] * 60);
	$end_date	= date("Y-m-d H:i:s", time());
}


// 获取渠道授权信息
$channel_id = 10;		// 京东商城
$channelinfo_obj = Factory::getModule('channelinfo', true);
$channelinfo_data = $channelinfo_obj->getChannelInfo($channel_id);


// 写日志
function writelog($msg)
{
	$msg = date("Y-m-d H:i:s") . " " . $msg . "\n";
	$filename = 'order_' . date("Y-m-d") . ".log";
	file_put_contents(dirname(__FILE__) . "/log/" . $filename, $msg, FILE_APPEND);
}


$jdApi2 = new JingdongApi2($channelinfo_data);
$condition = array();
$condition['method']		= '360buy.order.search';
$condition['order_state']	= 'WAIT_SELLER_STOCK_OUT,FINISHED_L,TRADE_CANCELED';	// 等待出库(已付款)，完成，取消
$condition['start_date']	= $start_date;
$condition['end_date']		= $end_date;
$condition['page']			= 1;
$condition['page_size']		= 1;
// $condition['optional_fields']	= 'order_id,vender_id,pay_type,order_total_price,order_payment,order_seller_price,freight_price,seller_discount,order_state,order_state_remark,delivery_type,invoice_info,order_remark,order_start_time,order_end_time,consignee_info,item_info_list,coupon_detail_list,modified';
$condition['optional_fields'] = 'order_id,order_state';

$orderinfo_data = $jdApi2->execute($condition);
if(!empty($orderinfo_data['order_search_response']['code'])){
	writelog("获取订单数据失败. start_date = {$start_date} end_date = {$end_date}");
	exit(1);
}

$page_size		= 20;
$page			= 1;
$order_total	= $orderinfo_data['order_search_response']['order_search']['order_total'];

if(empty($order_total)){
	writelog("无订单数据. start_date = {$start_date} end_date = {$end_date}");
	exit(0);
}

while(true)
{
	$condition['page']		= $page;
	$condition['page_size']	= $page_size;
	
	$orderinfo_data = $jdApi2->execute($condition);
	if(!empty($orderinfo_data['order_search_response']['code'])){
		writelog("获取订单数据失败. start_date = {$start_date} end_date = {$end_date}");
		exit(1);
	}
	
	if(empty($orderinfo_data['order_search_response']['order_search']['order_info_list'])){
		writelog("处理完成. page_size = {$page_size} page = {$page} start_date = {$start_date} end_date = {$end_date}");
		break;
	}
	
	writelog("page_size = {$page_size} page = {$page} start_date = {$start_date} end_date = {$end_date}");
	
	$order_info_list = $orderinfo_data['order_search_response']['order_search']['order_info_list'];
	
	foreach($order_info_list as $order_info){
		$order_id		= $order_info['order_id'];
		$order_state	= $order_info['order_state'];
		$data = array(
				'type'	=> 3,
				'data'	=> array(
					'type'			=> 'notify_trade',
					'order_id'		=> $order_id,
					'tid'			=> $order_id,
					'order_state'	=> $order_state,
					'channel_id'	=> $channel_id,				// 京东商城
				),
		);
		do_queue('taobaoorders', 'add', $data);
		writelog("order_id = {$order_id} order_state = {$order_state}");
	}
	
	$page += 1;
}


